﻿#include "TableLineEdit.h"
#include <QFocusEvent>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QTimer>
#include <QAbstractItemView>
#include <QStandardItem>
#include <QApplication>
#include <QClipboard>
#include <QDebug>


TableLineEdit::TableLineEdit(QWidget *parent)
    : QLineEdit{parent}
    , m_completer(nullptr)
    , m_enAC(true)
{
    init();
}

TableLineEdit::TableLineEdit(const QString &text, QWidget *parent)
    : QLineEdit{text, parent}
    , m_completer(nullptr)
    , m_enAC(true)
{
    init();
}

// 弹出菜单显示displays，实际在输入框显示values
void TableLineEdit::setAutoComplete(const QStringList &displays, const QStringList &values)
{
    if (m_completer) {
        m_completer->disconnect(this);           // 先断开原来的信号
        m_completer->model()->deleteLater();     // 释放模型
        m_completer->deleteLater();              // 交给 Qt 自动删除
        m_completer = nullptr;
    }

    QStandardItemModel* model = new QStandardItemModel(this);
    for (int i = 0; i < displays.length(); i++) {
        QStandardItem* item = new QStandardItem(displays.at(i));
        if (i < values.length()) {
            item->setData(values.at(i), Qt::UserRole + 1); // 把真实值存在 UserRole+1 中
        }
        model->appendRow(item);
    }

    m_completer = new QCompleter(model, this);
    m_completer->setCaseSensitivity(Qt::CaseInsensitive); // 不区分大小写
    // m_completer->setCompletionMode(QCompleter::PopupCompletion);
    // m_completer->setFilterMode(Qt::MatchContains);
    m_completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion); // 弹出列表
    m_completer->setCompletionRole(Qt::UserRole + 1); // 让 completer 自动填 UserRole+1 的值，而不是 DisplayRole

    setCompleter(m_completer);
}

void TableLineEdit::enableAutoComplete(bool en)
{
    m_enAC = en;
}

void TableLineEdit::setPasteCallback(QStringCallback_fn callack)
{
    m_callback = callack;
}

void TableLineEdit::init()
{
    // 背景透明以达到行颜色交替的效果
    setStyleSheet(
        "TableLineEdit {"
        "   border: none;"
        "   background: transparent;"
        "}"
        "TableLineEdit:focus {"
        "   border: 2px solid #0078d7;" // 聚焦时高亮边框
        "}"
    );

    // 只允许鼠标点击获得焦点，Tab/键盘不会自动聚焦
    setFocusPolicy(Qt::NoFocus);
    setContextMenuPolicy(Qt::CustomContextMenu);
}

void TableLineEdit::focusInEvent(QFocusEvent *e)
{
    QLineEdit::focusInEvent(e);
    if (m_completer && m_enAC) {
        m_completer->complete(QRect(0, -1 * height(), 300, 2 * height()));
    }
}

bool TableLineEdit::focusNextPrevChild(bool)
{
    // 返回 false，禁止 Qt 默认的 Tab 焦点切换
    return false;
}

void TableLineEdit::mousePressEvent(QMouseEvent *e)
{
    // 鼠标点击时才允许获得焦点
    setFocus();
    QLineEdit::mousePressEvent(e);
}

void TableLineEdit::keyPressEvent(QKeyEvent *e)
{
    if ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_V) {
        const QClipboard *cb = QApplication::clipboard();
        if (cb) {
            QString text = cb->text();
            if (m_callback) {
                insert(m_callback(text));
            } else {
                insert(text);
            }
        }
        return; // 处理完毕，不传给父类
    }
    switch (e->key()) {
    case Qt::Key_Up:
        emit focusMoveRequest(Top);
        e->accept();
        return;
    case Qt::Key_Down:
        emit focusMoveRequest(Bottom);
        e->accept();
        return;
    case Qt::Key_Tab:
        emit focusMoveRequest(Right);
        e->accept();
        return;
    case Qt::Key_Backtab: // Shift+Tab
        emit focusMoveRequest(Left);
        e->accept();
        return;
    case Qt::Key_Enter:
    case Qt::Key_Return:
        emit keyEnterRequest();
        e->accept();
        return;
    default:
        break;
    }
    QLineEdit::keyPressEvent(e);
}
